<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="pr-1 m-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({ op: 'add' })" v-auth="'sys:corpAdmin:edit'">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出风险点风险数据库索引清单') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ userCode: record.userCode, op: 'edit' })" :title="record.loginCode">
          {{ record.loginCode }}
        </a>
      </template>
      <template #corpColumn="{ record }">
        <a
          @click="reload({ searchInfo: { corpCode_: record.corpCode_ } })"
          :title="record.corpCode_"
        >
          {{ record.corpCode_ }}
        </a>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts" setup name="ViewsRiskPointList">
  import { onMounted, ref, unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { corpAdminDelete, corpAdminListData, corpAdminList } from '/@/api/sys/corpAdmin';
  import { resetpwd, corpAdminDisable, corpAdminEnable } from '/@/api/sys/corpAdmin';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import {officeTreeData} from "/@/api/sys/office";
  import {areaTreeData} from "/@/api/sys/area";

  const { t } = useI18n('sys.empUser');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const getTitle = {
    icon: meta.icon || 'simple-line-icons:badge',
    value: meta.title || t('租户管理员'),
  };
  const useCorpModel = ref<boolean>(false);
  const currentCorpCode = ref<string>('');
  const currentCorpName = ref<string>('');

  const searchForm: FormProps = {
    baseColProps: { lg: 4, md: 4 },
    labelWidth: 80,
    schemas: [
      {
        label: t('风险点名称'),
        field: 'loginCode',
        component: 'Input',
      },
      {
        label: t('管理部门'),
        field: 'testOffice.officeCode',
        component: 'TreeSelect',
        componentProps: {
          api: officeTreeData,
          allowClear: true,
        },
      },
      {
        label: t('所属区域'),
        field: 'testAreaCode',
        component: 'TreeSelect',
        componentProps: {
          api: areaTreeData,
          allowClear: true,
        },
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_user_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('车间/部门'),
      dataIndex: 'loginCode',
      key: 'a.login_code',
      sorter: true,
      width: 120,
      slot: 'firstColumn',
    },
    {
      title: t('风险点'),
      dataIndex: 'userName',
      key: 'a.user_name',
      sorter: true,
      width: 100,
    },
    {
      title: t('风险控制卡编号'),
      dataIndex: 'userName',
      key: 'a.user_name',
      sorter: true,
      width: 100,
    },
    {
      title: t('应用范围'),
      dataIndex: 'userName',
      key: 'a.user_name',
      sorter: true,
      width: 100,
    },
    {
      title: t('重大风险'),
      dataIndex: 'userName',
      key: 'a.user_name',
      sorter: true,
      width: 100,
    },
    {
      title: t('较大风险'),
      dataIndex: 'userName',
      key: 'a.user_name',
      sorter: true,
      width: 130,
    },
    {
      title: t('一般风险'),
      dataIndex: 'userName',
      key: 'a.user_name',
      sorter: true,
      width: 100,
    },
    {
      title: t('低风险（含轻微风险）'),
      dataIndex: 'userName',
      key: 'a.user_name',
      sorter: true,
      width: 120,
    },
  ];

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: corpAdminListData,
    beforeFetch: (params) => {
      return params;
    },
    immediate: false,
    columns: tableColumns,
    formConfig: searchForm,
    showIndexColumn: true,
    showTableSetting: true,
    useSearchForm: false,
    canResize: true,
  });

  onMounted(async () => {
    const res = await corpAdminList();
    useCorpModel.value = res.useCorpModel;
    currentCorpCode.value = res.currentCorpCode;
    currentCorpName.value = res.currentCorpName;
    reload();
  });

  function handleForm(record: Recordable) {
    if (record.op == 'add') {
      if (useCorpModel.value) {
        record.op = 'addCorp';
      } else {
        record.corpCode_ = currentCorpCode.value;
        record.corpName_ = currentCorpName.value;
        record.op = 'addAdmin';
      }
    }
    openDrawer(true, record);
  }

  async function handleDisable(record: Recordable) {
    const res = await corpAdminDisable(record);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleEnable(record: Recordable) {
    const res = await corpAdminEnable(record);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleDelete(record: Recordable) {
    const res = await corpAdminDelete(record);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleResetpwd(record: Recordable) {
    const res = await resetpwd(record);
    showMessage(res.message);
  }

  function handleSuccess(_record: Recordable) {
    reload();
  }
</script>
